/**
 * 
 */
package com.nus.iss.ad.dbhelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.nus.iss.ad.model.DepartmentInventoryDetailsModel;
import com.nus.iss.ad.model.RetrivalDetailsModel;
import com.nus.iss.ad.model.RetrivalListModel;
import com.nus.iss.ad.utils.JsonParser;
import com.nus.iss.ad.utils.MyConfig;

/**
 * @author Sachin
 * 
 */
public class RetrivalListDBHelper {

	static JSONArray json;

	public List<RetrivalListModel> getRetrivalList() throws JSONException {

		List<RetrivalListModel> rList = new ArrayList<RetrivalListModel>();
		RetrivalListModel robj = null;

		String url = MyConfig.BACKEND_PATH + "/api/GenerateRetrievalList";
		// url=url+"/"+poId;
		if (json == null) {
			json = JsonParser.getJSONArrayFromUrl(url);
		}
		for (int i = 0; i < json.length(); i++) {

			robj = new RetrivalListModel();
			robj.setItemName(json.getJSONObject(i).getJSONObject("inventory")
					.getString("description"));
			robj.setInventryId(json.getJSONObject(i).getJSONObject("inventory")
					.getString("inventoryId"));

			// robj.setRequired();

			robj.setRequired(json.getJSONObject(i).getInt("totalRequested"));

			int stock = json.getJSONObject(i).getJSONObject("inventory")
					.getInt("stock");

			if (stock > robj.getRequired()) {
				robj.setRetrivable(json.getJSONObject(i).getInt(
						"totalRequested"));
				robj.setCheckFlag(false);
			} else {
				robj.setRetrivable(json.getJSONObject(i)
						.getJSONObject("inventory").getInt("stock"));
				robj.setCheckFlag(true);
			}

			rList.add(robj);
		}

		return rList;
	}

	public RetrivalDetailsModel getRetrivalDetail(String inventoryId)
			throws JSONException {

		RetrivalDetailsModel rModel = new RetrivalDetailsModel();

		String url = MyConfig.BACKEND_PATH + "/api/GenerateRetrievalList";
		if (json == null) {
			json = JsonParser.getJSONArrayFromUrl(url);
		}
		List<DepartmentInventoryDetailsModel> deptModelList = new ArrayList<DepartmentInventoryDetailsModel>();

		DepartmentInventoryDetailsModel d = null;

		for (int i = 0; i < json.length(); i++) {

			if (json.getJSONObject(i).getJSONObject("inventory")
					.getString("inventoryId").equalsIgnoreCase(inventoryId)) {

				rModel.setItemName(json.getJSONObject(i)
						.getJSONObject("inventory").getString("description"));
				rModel.setTotalQuantity(json.getJSONObject(i).getInt(
						"totalRequested"));


				for (int j = 0; j < json.getJSONObject(i)
						.getJSONArray("department").length(); j++) {
					d = new DepartmentInventoryDetailsModel();

					JSONObject modelj = json.getJSONObject(i)
							.getJSONArray("department").getJSONObject(j);
					d.setDeptId(modelj.getString("deptId"));
					d.setDeptName(modelj.getString("name"));

					JSONObject reqdetailj = json.getJSONObject(i)
							.getJSONArray("requestList").getJSONObject(j);

					if (reqdetailj.getInt("qtyDelivered") == 0) {
						d.setQtyExpected(reqdetailj.getInt("qty"));
					} else {
						d.setQtyExpected(reqdetailj.getInt("qtyDelivered"));
					}

					d.setQtyRetrived(reqdetailj.getInt("qty"));

					deptModelList.add(d);

				}

				rModel.setDeptDetailList(deptModelList);
			}
		}

		return rModel;

	}

	public void updateJson(int position, int value, String selectedId)
			throws JSONException {

		// requestList

		for (int i = 0; i < json.length(); i++) {
			// JSONArray array = json.get

			if (json.getJSONObject(i).getJSONObject("inventory")
					.getString("inventoryId").equals(selectedId)) {

				JSONArray ar = json.getJSONObject(i)
						.getJSONArray("requestList");
				for (int j = 0; j < ar.length(); j++) {

					if (j == position) {
						ar.getJSONObject(j).put("qtyDelivered", value);
					}

				}

			}

		}

	}

	public void saveRetrivalList() {
		
		String url = MyConfig.BACKEND_PATH + "/api/confirmRetrievalList";
		JsonParser.postStream(url, json.toString());
		
		json = null;
		

	}
	

	public void checkData() throws JSONException {
		
		for (int i = 0; i < json.length(); i++) {
			// JSONArray array = json.get


				JSONArray ar = json.getJSONObject(i)
						.getJSONArray("requestList");
				for (int j = 0; j < ar.length(); j++) {

					
					if(ar.getJSONObject(j).getInt("qtyDelivered") == 0){
					int value = ar.getJSONObject(j).getInt("qty");
					ar.getJSONObject(j).put("qtyDelivered", value);

				}

			}

		}
		
	}
	
	
	public void addDate(Calendar c) throws JSONException {
		
		for(int i=0; i<json.length() ; i++){
			
			
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

			Date today = c.getTime();        
			String reportDate = df.format(today);

			// Print what date is today!
			System.out.println("Report Date: " + reportDate);
			
			
			json.getJSONObject(i).put("deliveryDate",reportDate);
			
			//Log.d("Test", date);
			
		}
		
		
		
		

	}
	
	public static void clearJson() {
		json = null;
	}

	

}